package com.skit.common.dialog;

import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.skit.common.R;
import com.skit.common.ResourcesManager;

/**
 * @author lgq
 * @version 1.0
 * @since 2020/3/8
 */
public class EditTextDialog extends DialogFragment {
    private OnEditTextDialogListener onEditTextDialogListener;
    private EditText editText;
    private TextView errorText;
    private int errorSoundID;
    private SoundPool soundPool;//声音池
    private boolean errorSound = true;
    private int maxLength = 0;

    public EditTextDialog() {
        //soundPool适合做简短的音效，优点为速度快可播放多个音效
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//如果设备sdk大于等于21
            soundPool = new SoundPool.Builder().setMaxStreams(2).build();
        } else {//小于21
            soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        }
        errorSoundID = soundPool.load(ResourcesManager.getAppContext(), R.raw.error_2, 1);//加载错误音效
        SharedPreferences setting = ResourcesManager.getAppContext().getSharedPreferences("setting", Context.MODE_PRIVATE);
        this.errorSound = setting.getBoolean("errorSound", true);
    }

    public interface OnEditTextDialogListener {
        void onDone(String text);

        void onCancel();
    }


    public void setMaxLength(int maxLength) {
        this.maxLength = maxLength;
    }

    public static EditTextDialog newInstance(String title) {
        Bundle args = new Bundle();
        args.putString("title", title);
        EditTextDialog fragment = new EditTextDialog();
        fragment.setArguments(args);
        return fragment;
    }

    public static EditTextDialog newInstance(String title, String hint) {
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("hint", hint);
        EditTextDialog fragment = new EditTextDialog();
        fragment.setArguments(args);
        return fragment;
    }

    public static EditTextDialog newInstance(String title, String hint, int textInputType) {
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("hint", hint);
        args.putInt("textInputType", textInputType);
        EditTextDialog fragment = new EditTextDialog();
        fragment.setArguments(args);
        return fragment;
    }

    public static EditTextDialog newInstance(String title, String hint, String text) {
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("hint", hint);
        args.putString("text", text);
        EditTextDialog fragment = new EditTextDialog();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.edittext_dialog, container, false);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView textView = view.findViewById(R.id.title);
        editText = view.findViewById(R.id.edit_text);
        errorText = view.findViewById(R.id.error_text);
        if (maxLength != 0) {
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
        }

        editText.setHint(getArguments().getString("hint", "请输入内容"));
        textView.setText(getArguments().getString("title"));
        String text = getArguments().getString("text", "");
        int textInputType = getArguments().getInt("textInputType", InputType.TYPE_CLASS_TEXT);
        editText.setText(text);
        editText.setInputType(textInputType);
        view.findViewById(R.id.ok_button).setOnClickListener(v -> {
            if (onEditTextDialogListener != null) {
                onEditTextDialogListener.onDone(editText.getText().toString());
            }
        });
        view.findViewById(R.id.cancel_button).setOnClickListener(v -> {
            if (onEditTextDialogListener != null) {
                onEditTextDialogListener.onCancel();
            }
            dismiss();
        });
    }

    public void setHint(String hint) {
        editText.setHint(hint);
    }

    public void setOnEditTextDialogListener(OnEditTextDialogListener onEditTextDialogListener) {
        this.onEditTextDialogListener = onEditTextDialogListener;
    }

    public void setErrorText(String text) {
        this.errorText.setText(text);
    }

    public void showError(String text) {
        this.errorText.setText(text);
        showError();
    }

    public void setSoundPool(SoundPool soundPool) {
        this.soundPool = soundPool;
    }

    public void showError() {
        errorText.setVisibility(View.VISIBLE);


        if (errorSound && soundPool != null) {
            soundPool.play(errorSoundID, 1, 1, 1, 0, 1);//播放错误音效
            //动画
            TranslateAnimation mTranslateAnimation = new TranslateAnimation(0, 0, 0, 5);
            mTranslateAnimation.setDuration(300);
            mTranslateAnimation.setFillAfter(true);
            errorText.setAnimation(mTranslateAnimation);
            errorText.startAnimation(mTranslateAnimation);
        }
    }

    public void hideError() {
        errorText.setVisibility(View.GONE);
    }

    //    @NonNull
//    @Override
//    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
//
//        return super.onCreateDialog(savedInstanceState);
//    }
}
